package com.hrms.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskOptions.Method;
import com.hrms.core.Constants.QueueType;
import com.hrms.service.SendMailService;

public class EmailMsg {	
	
	private static final Logger log = Logger.getLogger(EmailMsg.class.getName());	
	private List<String> recipients = new ArrayList<String>();
	private String subject;
	private String msg;
	//private boolean copyAdmins = false;
	private TemplateManager.Templates template;
	private Map<String, Object> params;
	private static final String ADMIN = "charmsstyler@gmail.com";
	private static final int QUOTA_LIMIT = 8;
	
	public Map<String, Object> getParams() {
		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	public TemplateManager.Templates getTemplate() {
		return template;
	}

	public void setTemplate(TemplateManager.Templates template) {
		this.template = template;
	}

	public EmailMsg(String to, String subject, String msg) {
		this.subject = subject;
		this.msg = msg;
		addRecipient(to);
	}

	public EmailMsg() {
		// no-op
	}

	public List<String> getRecipients() {
		return recipients;
	}
	public void setRecipients(List<String> recipients) {
		this.recipients = recipients;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public void addRecipient(String to) {
		if(!recipients.contains(to)){
			recipients.add(to);
		}
	}
	
	public void send() {
		log.info("has Recipients to Send -"+(recipients.size() > 0));
		if(recipients.size() > 0){					
			try {
				QueueType m = QueueType.Mail;
				Queue q = QueueFactory.getQueue(m.getType());
				TaskOptions taskOptions=null;				
				boolean resetReq = true;
				
					for (int i = 0; i < recipients.size(); i++) {
						
						if(resetReq){
							taskOptions = makeTaskOptions(m.getMapping());	
							resetReq = false;
						}
						
						taskOptions.param(SendMailService.TO.concat(String.valueOf(i)), recipients.get(i));
						
						if((( (i != 0)  && ((i+1)%QUOTA_LIMIT==0) ) || !((i+1) < recipients.size()) ) ){
							q.add(taskOptions);	
							resetReq = true;
						}
					}
					
					
					/*if(copyAdmins){
						TO.param(SendMailService.COPY_ADMINS, String.valueOf(copyAdmins));
					}*/

				
			} catch (Exception e) {
				log.log(Level.SEVERE, "Failed to Mail Q", e);
			}
		}
	}
	
	private TaskOptions makeTaskOptions(String mapping) {
		TaskOptions TO = TaskOptions.Builder.withUrl(mapping);
		TO.method(Method.POST);
		TO.param(SendMailService.SUBJECT, subject);
		if(template != null){
			msg  =  TemplateManager.getCompiledBody(template, params);							
		}
		
		TO.param(SendMailService.MSGBODY, msg);
		return TO;
	}

	public void copyAdmin(){
		//copyAdmins = true;
		addRecipient(ADMIN);
	}
	
}
